import java.util.HashMap;

/**
 * @author LKQ
 * @date 2022/2/15 14:22
 * @description 使用hashMap计数
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution formalSolution = new FormalSolution();
        int[] nums = {1,3,2,2,5,2,3,7};
        formalSolution.findLHS(nums);
    }
    public int findLHS(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int ans = 0;
        for (int i: nums) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }
        for (int key: map.keySet()) {
            if (map.containsKey(key + 1)) {
                ans = Math.max(ans, map.get(key) + map.get(key + 1));
            }
        }
        return ans;
    }
}
